var A;			// share/like node
var C;			// cursor
var D;			// dlg node
var E;			// command
var F;			// file/camera
var G;			// trash
var H;			// header
var I={			// personal info
	a:34,			// age
	c:'Hangzhou',	// city
	d:0,			// longitude
	e:0,			// latitude
	f:false,		// modified
	g:'Male',		// gender
	h:175,			// height
	n:'+',			// units
	p:'',			// password
	u:'',			// user
	w:75,			// weight
};
var J;			// share node
var K;			// like node
var M;			// home node
//var O=0;		// opt
var R;			// more
var S;			// session cookie
var T;			// out
var U;			// user cookie
//var X;			// xmlhttp

function cam2(){
	F.click();
}
function cam3(){
	var o=F.files[0];
	if(o){
		view(1);
		var f=new FormData();
		f.append('file',o);
	    var x=new XMLHttpRequest();
	    x.open('POST','/pad/upload',true);
	    x.onload=cam4;
	    x.send(f);
	}
}
function cam4(){
    flash(new Date().valueOf());
}
function click(e){
	if(e.target.tagName=='HTML' && document.body.className=='v0'){
		if(e.screenX*2>screen.width)
			_[69].fwd();
		else _[69].bwd();
	}
};
function cookies(){
	var u=null;
	var a=document.cookie.split(';');
	for(var i=a.length;i>0;i){
		var c=a[--i].trim();
		switch(c.substr(0,2)){
		case 'u=':
			u=c.substr(2);
		}
	}
	return u;
}
function dlg(d){
	dlgset();
	view(3);
	D.className='d'+d;
}
function dlgset(){
	dclass('d1',I.g);
	dlgval('d2',1,I.a);
	dlgval('d3',1,I.h);
	dlgval('d3',2,I.w);
	dlgval('d4',1,I.u);
	dlgval('d4',2,I.p);
	var o=cookies()?'out':'in';
	document.getElementById('sio').innerHTML='<a href=javascript:sign'+o+
		'()><div class=go id=sig>&nbsp;</div>Sign'+o+'</a>';

}
function dclass(d,c){
	document.getElementById(d).className=c;
}
function dummy(){
}
function dlgval(d,n,v){
	document.getElementById(d).children[n].firstChild.firstChild.value=v;
}
function flash(){
	var x=new XMLHttpRequest();
	x.timeout=8000;
	x.ontimeout=function(){
		home();
	}
	x.onload=function(){
		try{
			var s=eval(x.responseText);
			if(s[0].k>_[69].get()[0].k){
				localStorage.setItem('s',JSON.stringify(s));
				_[69].set(s);
			}
		}finally{}
		home();
	}
	x.open('GET','/pad/pad.json',true);
	x.send();
}
function geoerr(e){
	switch(e.code){
	case e.PERMISSION_DENIED:
		out('User denied the request for Geolocation.');
		break;
	case e.POSITION_UNAVAILABLE:
		out('Location information is unavailable.');
		break;
	case e.TIMEOUT:
		out('The request to get user location timed out.');
		break;
	case e.UNKNOWN_ERROR:
		out('An unknown error occurred.');
		break;
	}
}
function geoinf(p){
	I.d=p.coords.longitude;
	I.e=p.coords.latitude;
	localStorage.setItem('i',JSON.stringify(I));
	dlgset();
}
function infcookie(){
	var c=(I.a?I.a:0)+'*'+(I.c?I.c:'+')+'*'+(I.d?I.d:0)+'*'+(I.e?I.e:0)+'*'+
			(I.g?I.g:'+')+'*'+(I.h?I.h:0)+'*'+(I.n?I.n:'+')+'*'+(I.w?I.w:0);
	var d=new Date();
	d.setTime(d.getTime()+(1000*86400000));	// 1000 days
	document.cookie='i='+c+'; expires='+d.toGMTString()+'; path=/';

}
function info(d,n){
	return document.getElementById(d).children[n].firstChild.firstChild.value;
}
function mtext(d,x){
	R.firstChild.children[d].firstChild.firstChild.textContent=x;
}
function out(v){
	T.textContent=v;
}
function post(c){
	M.className=c.k?'g0':null;
	var r=c.r;
	if(r>15){		// share
		A.className='sh';
		J.className=r>20?'shr':'ush';
	}else{			// like
		A.className='lk';
		K.className=r>13?'dsl':'lke';
	}
	C=c;
}
function rate(r){
	var x=new XMLHttpRequest();
	x.onreadystatechange=dummy;
	x.open('POST','.rate?i='+C.s+'&r='+r,true);
	x.send();
	C.r=r;
	localStorage.setItem('s',JSON.stringify(_[69].get()));
}
function sign(io){
	var n=document.getElementById('sig');
	n.className='wt';
	var x=new XMLHttpRequest();
	x.open('POST','/sys/sign'+io+'?account='+document.getElementById('sia').value+
			'&password='+document.getElementById('sip').value+'&ok=&jump=none',
			true);
	x.onload=function(){
		n.className='go';
		x=x.responseText;
		if(x=='Ok'){
			more();
			C=null;
		}else out(x);
	}
	x.send();
}
function touchend(e){
	var t=e.target;
	while(t){
		if(t.tagName=='A'){
			var r=t.href;
			if(r.substr(0,11)=='javascript:')
				eval(r.substr(11));
			e.preventDefault();
			return;
		}
		t=t.parentNode;
	}
}
function touchmove(e){
	e.preventDefault();
}
function view(v){
	document.body.className='v'+v;
}

function about(){
	dlg(6);
}
function account(){
	dlg(4);
}
function act(){
}
function age(){
	dlg(2);
}
function camera(){
	setTimeout(cam2,1);
}
function city(){
	dlg(0);
}
function dislike(){
	rate(12);
	post(C);
}
function female(){
	I.g='Female';
	more();
}
function gender(){
	dlg(1);
}
function height(){
	dlg(3);
}
function home(){
	if(!C)
		flash();
	if(document.body.className=='v0')
		_[69].rst();
	else view(0);
}
function like(){
	rate(C.r>13?12:14);
	post(C);
}
function male(){
	I.g='Male';
	more();
}
function more(){
	I.a=info('d2',1);
	I.h=info('d3',1);
	I.w=info('d3',2);
	I.u=info('d4',1);
	I.p=info('d4',2);
	mtext(1,I.c);
	mtext(2,I.g);
	mtext(3,I.a);
	mtext(4,I.h+' cm / '+I.w+' kg');
	mtext(6,I.u);
	localStorage.setItem('i',JSON.stringify(I));
	view(2);
}
function recyclebin(){
	view(4);
}
function share(){
	rate(C.r>20?18:60);
	post(C);
}
function signin(){
	sign('in','out');
}
function signout(){
	sign('out','in');
}
function signup(){
	dlg(7);
}
function register(){
	more();
}
function units(){
	dlg(8);
}
function unshare(){
	rate(60);
	post(C);
}

A=document.getElementById('ac');
D=document.getElementById('v3');
F=document.getElementById('cam');
J=document.getElementById('shr');
K=document.getElementById('lke');
M=document.getElementById('g0');
R=document.getElementById('v2');
T=document.getElementById('out');

navigator.geolocation.getCurrentPosition(geoinf,geoerr);
document.onclick=click;
document.ontouchend=touchend;
document.ontouchmove=touchmove;
F.onchange=cam3;

c=_[66];
c._(document.getElementById('v4'));

c=_[69];
c._(document.getElementById('v0'));
c.pst=post;
try{
	var x;
	if(x=localStorage.getItem('s'))
		c.set(JSON.parse(x));
	if(x=localStorage.getItem('i'))
		I=JSON.parse(x);
}finally{
	infcookie();
	dlgset();
	flash();
}